import { defineStore } from 'pinia'
import {listRoutes, listRoutesTree, listUserRoutes} from "@/api/backend/route/route.js";
import {addRoutes, buildRouteTree, treeify} from "@/api/util/route.js";
import lodash from "lodash";
let userRouteStore=defineStore("routeStore",{

    state:()=>{
        return {
            routes: [],
            routeMap:{}
        }
    },
    actions : {
        async fetchRoutes(router){
            const result=await listUserRoutes()

            const data=result.data


            data.forEach(item=>{
                this.routeMap[item.name]=item
            })

            const roots=data.filter(item=>item.parentId===0)

            treeify(roots,data)

            this.routes=roots


            return addRoutes(null,buildRouteTree(lodash.cloneDeep(this.routes)),router)
        },

        clear(){
            this.routes=[]
            this.routeMap={}
        }



    },
    getters:{}

})

export default userRouteStore